<?php
declare (strict_types=1);

namespace app\wap\controller;

use mumbaicat\apidoc\ApiDoc;
use think\Response;

class Index extends Base
{
    protected $noLoginAction = ['*'];

    public function index()
    {
        return '您好！这是一个[wap]示例应用';
    }

    /***
     * doc #
     *
     * @auth kouweihong
     * @date 2020/11/30
     */
    public function doc()
    {
        require $this->app->getRootPath() . '/vendor/mumbaicat/makeapidoc/src/mumbaicat/apidoc/ApiDoc.php';

        $documentPath = __DIR__;
        $doc = new ApiDoc($documentPath);
        $doc->setTimes(100, 100);
        echo $doc->make(true);
    }

    /**
     * login #登录
     * api post /{controller}/login
     * @param username string 用户名
     * @param password string 密码
     *
     * @auth kouweihong
     * @date 2020/11/30
     * @throws \Exception
     */
    public function login()
    {
        $this->request->isPost() || $this->error('非法操作');
        $username = input('post.username');
        $password = input('post.password');
        (!$username || !$password) && $this->error('账户密码错误');

        //查询用户信息
        $password = md5(md5($password));
        $user = \app\common\model\User::getInfoByWhere(['user_type' => \app\common\model\User::USER_TYPE_10, 'username' => $username, 'password' => $password]);
        $user->isEmpty() && $this->error('账户密码不存在');
        $user->token = \Random::uuid();

        //更新用户信息
        $data = [
            'login_ip' => $this->request->ip(),
            'login_time' => time(),
            'login_token' => $user->token,
            'login_token_expire' => time() + 7200,
        ];
        $res = $user->save($data);
        $res === false && $this->error('登录失败');

        $this->success($user->toArray());
    }
}
